
#include "stdint.h"
#include "main.h"
#include "stdio.h"
#include "string.h"


extern uint16_t NODE_ADDR;
uint8_t  frame_crc(uint8_t* msg)
{
    #define  FRAMR_LENGTH    7

    uint8_t ret=0;
    for(int i=0; i<FRAMR_LENGTH; i++)
    {
        ret += msg[i];
    }
    ret = ~ret;

    return ret;
}

void sendback_UVP(uint32_t reg1,uint32_t reg2)
{
    uint16_t  volt = (uint16_t)((double)(reg1>>16)/10);
    uint32_t  current_x10 = (uint32_t)((double)(reg2>>16)/25.6f);
    uint32_t  power_x10  = (uint32_t)(((double)(reg2 & 0x0000FFFF)/64)*10.0f) ;

    uint8_t  data[10]={0x55,0xaa,NODE_ADDR,0x40,
                        volt,
                       (current_x10&0x0000ff00)>>8,current_x10&0x000000ff,
                       (power_x10&0x0000ff00)>>8,power_x10&0x000000ff,
                       0xff};

    data[9] = frame_crc(data+2);

    UART1_485_TXEN();
    for(int i=0;i<10;i++){
        fputc(data[i],&__stdout);
    }
    UART1_485_RXEN();
}


void sendback_speed(uint32_t val1, uint32_t  val2)
{
    uint16_t  duty_x10 = (uint16_t)(((double)val1/1073741824)*1000);
    uint16_t  rpm_x10 = (uint16_t)(val2&0x0000FFFF);

    uint8_t  data[10]={0x55,0xaa,NODE_ADDR,0x41,
                        (duty_x10&0xff00)>>8, duty_x10&0x00ff,
                        (rpm_x10&0xff00)>>8, rpm_x10&0x00ff,
                        0,0xff};
    
    data[9] = frame_crc(data+2);

    UART1_485_TXEN();
    for(int i=0;i<10;i++){
        fputc(data[i],&__stdout);
    }
    UART1_485_RXEN();

}



void sendback_fault_info(uint32_t  data1,uint32_t  data2)
{
    uint8_t data[10]={0x55,0xaa,NODE_ADDR,0x43,
                        data1>>24,
                        0,0,0,0,
                        0xff };

    memcpy(data+5, &data2, 4);
    
    data[9] = frame_crc(data+2);

    UART1_485_TXEN();
    for(int i=0;i<10;i++){
        fputc(data[i],&__stdout);
    }
    UART1_485_RXEN();

}


void sendback_nodeID_changed(void)
{
    uint8_t data[10]={0x55,0xaa,NODE_ADDR,9,
                       1,0,0,0,0,0xff};

    data[9] = frame_crc(data+2);

    UART1_485_TXEN();
    for(int i=0;i<10;i++){
        fputc(data[i],&__stdout);
    }
    UART1_485_RXEN();

}


void sendback_falut_cleared(void)
{
    uint8_t data[10]={0x55,0xaa,NODE_ADDR,0x44,
                        1,0,0,0,0,0xff};

    data[9] = frame_crc(data+2);

    UART1_485_TXEN();
    for(int i=0;i<10;i++){
        fputc(data[i],&__stdout);
    }
    UART1_485_RXEN();

}


void sendback_duty_info(uint8_t dir)
{
    uint8_t data[10]={0x55,0xaa,NODE_ADDR,0x16,
                        dir,1,0,0,0,0xff};

    data[9] = frame_crc(data+2);

    UART1_485_TXEN();
    for(int i=0;i<10;i++){
        fputc(data[i],&__stdout);
    }
    UART1_485_RXEN();

}


void sendback_rom_data(uint8_t reg, uint32_t data2)
{
    uint8_t data[10]={0x55,0xaa,NODE_ADDR,3,
                        reg,
                       0,0,0,0,
                       0xff};

    memcpy(data+5, &data2, 4);
    
    data[9] = frame_crc(data+2);

    UART1_485_TXEN();
    for(int i=0;i<10;i++){
        fputc(data[i],&__stdout);
    }
    UART1_485_RXEN();

}


